//
//  HNHappyFamilyMusicWorldCell.m
//  XDEducation
//
//  Created by 星道 on 17/6/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyFamilyMusicWorldCell.h"
#import "HNAudioPlayer.h"

@implementation HNHappyFamilyMusicWorldCell
{
    IBOutlet UIButton *_DownBtn;
    IBOutlet NSLayoutConstraint *_DownBtnConstraint;
    IBOutlet UIButton *_PlayBtn;
    IBOutlet UILabel *_Title;
}

- (IBAction)DownLoad:(UIButton *)sender
{
    //已下载则不操作
    if (!self.Model.IsReal)
    {
        self.ClickDownLoad (self.Model);
    }
}
- (IBAction)PlayAudio:(UIButton *)sender
{
    
    
    HNHappyFamilyMusicWorldModel *xx = nil;
    xx = self.Model?:self.MusicModel;
    NSURL *Url = nil;
    if (self.Model)
    {
        Url = [NSURL URLWithString:self.Model.absoluteUrl];
    }
    else
    {
        Url = [NSURL fileURLWithPath:self.MusicModel.absoluteUrl];
    }
    
    
    sender.selected = xx.IsPlay = !xx.IsPlay;
    if (sender.selected)
    {
        [HNAudioPlayer ShareAudioPlayerWithUrlStr:Url];
    }
    else
    {
        [HNAudioPlayer Close];
    }
    
    self.ClickPlayer (xx,!sender.selected);
    
}



//已下载用
- (void)setMusicModel:(HNHappyFamilyMusicWorldModel *)MusicModel
{
    _MusicModel = MusicModel;
    _Title.text = MusicModel.musicTitle;
    _DownBtnConstraint.constant = 0;
    _PlayBtn.selected = MusicModel.IsPlay;
}



//下载用
- (void)setModel:(HNHappyFamilyMusicWorldModel *)Model
{
    _Model = Model;
    _Title.text = Model.musicTitle;
    
    _PlayBtn.selected = Model.IsPlay;
    _DownBtn.selected = Model.IsReal;
    //正在下载时显示进度
    if (Model.Progress > 0)
    {
        [_DownBtn setTitle:[NSString stringWithFormat:@"%.0f%@",Model.Progress*100,@"%"] forState:(UIControlStateNormal)];
    }
    else
    {
        [_DownBtn setTitle:@"下载" forState:(UIControlStateNormal)];
    }
    
    //已下载则不点击
    if (Model.IsReal)
    {
        _DownBtn.userInteractionEnabled = NO;
    }
    else
    {
        _DownBtn.userInteractionEnabled = YES;
    }
    
}
@end
